﻿using System;
using System.Linq;
using Ayatta.Contract;
using Ayatta.Contract.Domain;

namespace Web.Base.OrderHandler
{
    internal sealed class FinalHandler : IOrderHandler
    {
        private readonly IOrderService orderService;

        public FinalHandler()
        {
            orderService = Application.Container.GetInstance<IOrderService>();
        }

        public void Process(UserTrade trade, Order order)
        {
            var count = order.OrderItems.Count(o => o.Status == OrderStatus.Finished);
            var total = order.OrderItems.Count(o => o.Status != OrderStatus.Canceled || o.Status != OrderStatus.Deleted);
            if (count == total)
            {
                order.Status = OrderStatus.Finished;
            }
            var status = orderService.OrderAutoFinished(trade, order);
            if (!status)
            {
                Application.Logger.Warn("FinalHandler：失败");
            }
        }
    }
}